<?php

namespace app\admin\controller;

use app\model\Guide as guideModel;

class Guide extends Base
{
    protected function _infoModule()
    {
        return array(
            'info' => array(
                'name' => '引导管理',
                'description' => '管理网站后台管理员',
            ),
            'menu' => array(
                array(
                    'name' => '列表',
                    'url' => url('guide/index'),
                    'icon' => 'list',
                ),
            ),
            '_info' => array(
                array(
                    'name' => '添加',
                    'url' => url('guide/info'),
                ),
            ),
        );
    }

    public function index()
    {
        $request = $this->request;

        //筛选条件
        $where = [];
        $pageMaps = [];

        $id = $request->post('id');
        $status = $request->post('status');

        if ($id) {
            $where['id'] = $id;
        }
        if ($status) {
            $where['status'] = $status;
        }

        //URL参数
        $pageMaps['id'] = $id;
        $pageMaps['status'] = $status;

        $pageAry = [
            'list_rows' => 10,
            'page' => $request->get('page', 1),
            'path' => 'index',
            'query' => $pageMaps,
        ];

        //查询数据
        $guideModel = new guideModel;
        $list = $guideModel->loadList($where, $pageAry);
        $this->assign('list', $list);
        $this->assign('_page', $list->render());
        $this->assign('pageMaps', $pageMaps);

        $this->fetch('index');
    }

    public function info()
    {
        $request = $this->request;
        $method = $request->method();
        $guideModel = new guideModel;
        if ($method == 'POST') {
            $id = $request->post('id');
            if ($id) {
                $status = $guideModel->editAll($request);
            } else {
                $status = $guideModel->addall($request);
            }
            if ($status !== false) {
                jsonApi(1, '操作成功', ['url'=>url('guide/index')]);
            } else {
                jsonApi(0, '操作失败');
            }
        } else {
            $id = $request->get('id');
            $info = $guideModel->getInfo($id);
            $this->assign('info', $info);

            $this->fetch('info');
        }
    }

    /**
     * 删除信息
     */
    public function del()
    {
        $request = $this->request;
        $id = $request->post('id');
        if (empty($id)) {
            jsonApi(0, '参数不能为空');
        }

        if (model('Guide')->del($id)) {
            jsonApi(1, '删除成功！');
        } else {
            jsonApi(0, '删除失败');
        }
    }

}
